<?php
/**
 * Created by PhpStorm.
 * User: THINK
 * Date: 2017/8/25
 * Time: 17:47
 */

namespace Admin\Controller;
use Think\Controller;


class PictureController extends Controller
{
    public function index(){
        $v = I('keyword');
        $User = M('Picture');
        $count = $User->where('aid=1')->count();
        $Page = new \Think\Page($count,10);
        $show  = $Page->show();
        $list = $User->field('a.id,a.title,a.publishtime,a.click,a.cid,b.name')->alias('a')->join('xyh_category b on a.cid=b.id','left')->where('a.title like "%'.$v.'%" and aid=1')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->assign('count',$count);
        $this->display();
    }


    public function del(){
        $id=is_array(I('id'))?implode(",", I('id')):I('id');
        $data['aid']=0;
        $sun = M('Picture')->where('id in('.$id.')')->save($data);
        if (is_array(I('id'))){
            echo $sun > 0 ? 1 : 0;
        }else{
            if ($sun > 0) {
                $this->success('删除成功', U('Picture/index',array('id'=>I('pid'))));
            } else {
                $this->success('删除失败', U('Picture/index',array('id'=>I('pid'))));
            }
        }
    }


    public function trach(){
        $User = M('Picture');
        $count = $User->where('aid=0')->count();
        $list = $User->field('a.id,a.title,a.publishtime,a.click,a.cid,a.aid,b.name')->alias('a')->join('xyh_category b on a.cid=b.id','left')->limit($Page->firstRow.','.$Page->listRows)->where('a.title like "%'.$v.'%" and aid=0')->order('a.id desc')->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->display();
    }


    public function haiy(){
        $id=is_array(I('id'))?implode(",", I('id')):I('id');
        $data['aid']=1;
        $sun = M('Picture')->where('id in('.$id.')')->save($data);
        if (is_array(I('id'))){
            echo $sun > 0 ? 1 : 0;
        }else{
            if ($sun > 0) {
                $this->success('删除成功', U('Picture/index',array('id'=>I('pid'))));
            } else {
                $this->success('删除失败', U('Picture/index',array('id'=>I('pid'))));
            }
        }
    }



    public function delete()
    {
        $User = M('Picture');
        $id = I('id');
        $id = is_array(I('id')) ? implode(',', I('id')) : I('id');
        if ($User->delete($id) !== false) {
            $this->success('删除成功', U('Picture/trach',array('id'=>I('pid'))));
        } else {
            $this->error('删除失败');
        }
    }


    public function move(){
        $id = I('id');
        $User = M('category');
        $Picture = M('Picture');
        $list = $User->where('modelid=4')->select();
        $lists = $Picture->find($id);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        $this->display();

    }


    public function moves(){
        $User = M('Picture');
        $cid = I('cid');
        $User->where('id in('.I('id').")")->save(array('cid'=>I('cid')));
        $this->redirect('Picture/index');

    }

}